package com.osce.common.utils.oesc;

import com.doudou.core.container.ListUtil;
import com.doudou.core.container.MapUtil;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * @author zzxjs
 * @since 2023/9/8
 * osce分配考站的响应对象
 * 使用map的结构，key为date，value为当前考试的所有学员
 */
public class OsceAllotResponse extends LinkedHashMap<String, LinkedHashMap<Long, LinkedList<PlaceModel>>> {

    private OsceAllotRequest request;

    public OsceAllotResponse() {}

    public OsceAllotResponse(OsceAllotRequest request) {
        this.request = request;
    }

    public OsceAllotRequest getRequest() {
        return request;
    }

    public void setRequest(OsceAllotRequest request) {
        this.request = request;
    }

    public LinkedHashMap<Long, LinkedList<PlaceModel>> get(String key){
        return super.get(key);
    }

    /**
     * 添加这个日期的排站
     * @param date 日期
     * @param placeAllotMap 排站结果  key:考站id   value：考站集合
     */
    public void addPlaceAllot(String date,Map<Long, LinkedList<PlaceModel>> placeAllotMap){
        LinkedHashMap<Long, LinkedList<PlaceModel>> placeAllotBeans;
        if (ListUtil.isEmpty(placeAllotBeans = get(date))){//查询容器是否存在此日期数据，不存在则创建
            placeAllotBeans = new LinkedHashMap<>();
        }
        for (Map.Entry<Long, LinkedList<PlaceModel>> entry : placeAllotMap.entrySet()) {//遍历传递进来的数据
            Long placeId = entry.getKey();
            LinkedList<PlaceModel> models = entry.getValue();
            LinkedList<PlaceModel> thisModels = placeAllotBeans.get(placeId);//查询此日期此考站的数据
            if (ListUtil.isEmpty(thisModels)){//说明是第一次添加，直接赋值
                thisModels = models;
            }else {//如果不是第一次添加，需要追加
                for (int i = 0; i < models.size(); i++) {
                    PlaceModel thisModel = thisModels.get(i);
                    PlaceModel model = models.get(i);
                    thisModel.addAllPlaceAllot(model.getPlaceAllotList());
                    thisModels.set(i,thisModel);
                }
            }
            placeAllotBeans.put(placeId,thisModels);
        }
        put(date,placeAllotBeans);
    }



    /**
     * 格式化添加的房间模型
     */
    private Map<Long,List<PlaceAllotBean>> parsePlaceAllotList(PlaceModel model){
        Map<Long,List<PlaceAllotBean>> map = MapUtil.map(true);
            if (map.containsKey(model.getId())){
                List<PlaceAllotBean> beans = map.get(model.getId());
                beans.addAll(model.getPlaceAllotList());
                map.put(model.getId(),beans);
            }else {
                map.put(model.getId(),model.getPlaceAllotList());
            }
        return map;
    }

}
